package com.lz.service.lg.impl;

import com.lz.entity.Car;
import com.lz.entity.Customer;
import com.lz.entity.Employee;
import com.lz.entity.OrderSheet;
import com.lz.repository.lg.OrderSheetDao;
import com.lz.repository.lg.VO.OrderSheetVO;
import com.lz.service.lg.OrderSheetService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class OrderSheetServiceImpl implements OrderSheetService {

	//自动注入session
	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private OrderSheetDao orderSheetDao;
	@Override
	public Integer addOrderSheet(OrderSheet orderSheet, Customer customer, Car car, Employee employee) {
		//开启事务
		Session session = sessionFactory.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		orderSheet.setCar(car);
		orderSheet.setEmployee(employee);
		orderSheet.setCustomer(customer);
		car.setCustomer(customer);
		orderSheetDao.addCar(session, car);
		orderSheetDao.addCustomer(session, customer);
		orderSheetDao.addorderSheet(session, orderSheet);
		//提交事务
		transaction.commit();
		return null;
	}
	
	@Override
	public List<OrderSheetVO> selectOrderSheet(String customerName,Integer pageNo,Integer pageSize,String brand) {
		Session session = sessionFactory.openSession();
		StringBuffer hql = new StringBuffer("from OrderSheet os where 1=1 ");
		Query query = session.createQuery(hql.toString());
		//如果客户姓名查询不为空
		if(customerName != null && !"".equals(customerName)){
			//客户姓名不为空的同时,汽车品牌也不为空
			if(brand != null && !"".equals(brand)){
				hql.append(" and os.customer.customerName like :customerName and os.car.brand like :brand");
				query = session.createQuery(hql.toString());
				query.setString("customerName", "%"+customerName+"%");
				query.setString("brand", "%"+brand+"%");
			}else{
				//只有用户名不为空
				hql.append(" and os.customer.customerName like :customerName");
				query = session.createQuery(hql.toString());
				query.setString("customerName", "%"+customerName+"%");
			}
		}else{
			//用户名为空,汽车品牌不为空
			if(brand != null && !"".equals(brand)){
				hql.append(" and os.car.brand like :brand");
				query = session.createQuery(hql.toString());
				query.setString("brand", "%"+brand+"%");
			}
		}
		query.setFirstResult((pageNo-1)*pageSize);
		query.setMaxResults(pageSize);
		List<OrderSheet> list = orderSheetDao.selectOrders(query);
		List<OrderSheetVO> voList = new ArrayList<>();
		for (OrderSheet orderSheet : list) {
			OrderSheetVO orderSheetVO2 = new OrderSheetVO();
			Integer orderId = orderSheet.getOrderId();
			orderSheetVO2.setOrderSheetId(String.valueOf(orderId));
			orderSheetVO2.setCustomerName(orderSheet.getCustomer().getCustomerName());
			orderSheetVO2.setCustomerTel(orderSheet.getCustomer().getCustomerTel());
			orderSheetVO2.setBrand(orderSheet.getCar().getBrand());
			orderSheetVO2.setVin(orderSheet.getCar().getVin());
			orderSheetVO2.setOrderState(orderSheet.getOrderState());
			Double price = orderSheet.getCar().getPrice();
			orderSheetVO2.setPrice(String.valueOf(price));
			Date orderSheetDate = orderSheet.getOrderSheetDate();
			String format = new SimpleDateFormat("yyyy-MM-dd").format(orderSheetDate);
			orderSheetVO2.setMinopeningTime(format);
			orderSheetVO2.setOrderState(orderSheet.getOrderState());
			voList.add(orderSheetVO2);
		}
		return voList;
	}
	@Override
	public Integer rowsOfOrderSheet(String customerName,String brand) {
		Session session = sessionFactory.openSession();
		StringBuffer hql = new StringBuffer("from OrderSheet os where 1 = 1 ");
		Query query = session.createQuery(hql.toString());
		//如果客户姓名不为空
		if(customerName != null && !"".equals(customerName)){
			//如果汽车品牌不为空
			if(brand != null && !"".equals(brand)){
				hql.append(" and os.customer.customerName like :customerName and os.car.brand like :brand");
				query = session.createQuery(hql.toString());
				query.setString("customerName", "%"+customerName+"%");
				query.setString("brand", "%"+brand+"%");
			}else{
				//客户姓名不为空,汽车品牌为空
				hql.append(" and os.customer.customerName like :customerName");
				query = session.createQuery(hql.toString());
				query.setString("customerName", "%"+customerName+"%");
			}
		}else{
			//客户姓名为空,汽车品牌不为空
			if(brand != null && !"".equals(brand)){
				hql.append(" and os.car.brand like :brand");
				query = session.createQuery(hql.toString());
				query.setString("brand", "%"+brand+"%");
			}
		}
		return orderSheetDao.rowsOfOrderSheet(query);
	}
	@Override
	public Integer rowsByMap(Map map) {
		Session session = sessionFactory.openSession();
		StringBuffer stringBuffer = new StringBuffer("from OrderSheet os where 1=1 ");
		//通过迭代器遍历map
		Iterator iterator = map.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry next = (Map.Entry<String, String>)iterator.next();
			String key = (String)next.getKey();
			String value = (String)next.getValue();
			if(!"".equals(value)){
				stringBuffer.append("and os.");
				stringBuffer.append(key);
				stringBuffer.append(" like '%");
				stringBuffer.append(value);
				stringBuffer.append("%' ");
			}
		}
		Query query = session.createQuery(stringBuffer.toString());
		Integer rows = orderSheetDao.rowsOfOrderSheet(query);
		return rows;
	}
	@Override
	public void deleteOrderSheet(Integer orderSheetId) {
		Session session = sessionFactory.getCurrentSession();
		OrderSheet orderSheet = (OrderSheet)session.get(OrderSheet.class, orderSheetId);
		session.delete(orderSheet);
		session.flush();
	}
	
	@Override
	public void submitOrderSheet(Integer orderSheetId) {
		Session session = sessionFactory.getCurrentSession();
		OrderSheet orderSheet = (OrderSheet)session.get(OrderSheet.class, orderSheetId);
		String orderState = orderSheet.getOrderState();
		System.out.println(orderState);
		orderSheet.setOrderState("已提交");
		session.update(orderSheet);
		session.flush();
	}
	@Override
	public Map<String, Object> findByorderSheetId(Integer orderSheetId) {
		//开启会话
		Session session = sessionFactory.getCurrentSession();
		String hql = "from OrderSheet os where os.orderId = "+String.valueOf(orderSheetId);
		//查询 到对应的订单
		OrderSheet orderSheet = orderSheetDao.findOrderSheet(session, hql);
		Map<String, Object> map = new HashMap<>();
		map.put("orderSheet", orderSheet);
		map.put("car", orderSheet.getCar());
		map.put("customer", orderSheet.getCustomer());
		return map;
	}

	@Override
	public void changeOrderSheet(OrderSheetVO orderSheetVO) {
		Session session = sessionFactory.getCurrentSession();
		String orderSheetId = orderSheetVO.getOrderSheetId();
		Map<String, Object> map = findByorderSheetId(Integer.parseInt(orderSheetId));
		//根据订单主键分别查出订单,客户和车辆信息
		OrderSheet orderSheet = (OrderSheet)map.get("orderSheet");
		Car car = (Car)map.get("car");
		Customer customer = (Customer)map.get("customer");
		orderSheet.setOrderMoney(Integer.parseInt(orderSheetVO.getTotalPrice()));
		customer.setCustomerCardId(orderSheetVO.getCustomerCardId());
		customer.setCustomerName(orderSheetVO.getCustomerName());
		customer.setCustomerTel(orderSheetVO.getCustomerTel());
		customer.setCustomerAddress(orderSheetVO.getCustomerAddress());
		customer.setSex(orderSheetVO.getSex().charAt(0));
		car.setBrand(orderSheetVO.getBrand());
		car.setSeries(orderSheetVO.getSeries());
		car.setModel(orderSheetVO.getModel());
		car.setConfigure(orderSheetVO.getConfigure());
		car.setColor(orderSheetVO.getColor());
		car.setPrice(Double.parseDouble(orderSheetVO.getPrice()));
		car.setVin(orderSheetVO.getVin());
		session.update(orderSheet);
		session.update(car);
		session.update(customer);
		session.flush();
	}

	@Override
	public Integer selectMaxOrderId() {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from OrderSheet os order by os.orderId";
		Query query = session.createQuery(hql);
		List list = query.list();
		OrderSheet o = (OrderSheet)list.get(list.size() - 1);
		System.out.println(o.getOrderId());
		return o.getOrderId();
	}


}
